//package com.service.impl;
//import com.pojo.Employee;
//import com.pojo.Workstation;
//import com.pojo.EmployeeStatus;
//import com.pojo.WorkstationStatus;
//import com.mapper.EmployeeMapper;
//import com.mapper.WorkstationMapper;
//import com.service.EmployeeService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.Optional;
//
//@Service
//public class EmployeeServiceImpl implements EmployeeService {
//    @Autowired
//    private EmployeeMapper employeeMapper;
//    @Autowired
//    private WorkstationMapper workstationMapper;
//
//    @Override
//    public void add(Employee employee) {
//        employeeMapper.insert(employee);
//    }
//
//    @Override
//    public Employee findById(Integer id) {
//        return Optional.ofNullable(employeeMapper.selectById(id))
//                .orElseThrow(() -> new RuntimeException("员工不存在"));
//    }
//
//    @Override
//    public void update(Employee employee) {
//        employeeMapper.updateById(employee);
//    }
//
//    @Transactional
//    @Override
//    public void assignWorkstation(Integer employeeId, Integer workstationId) {
//        // 1. 查询员工和工位
//        Employee employee = findById(employeeId);
//        Workstation workstation = workstationMapper.selectById(workstationId);
//
//        // 2. 验证工位合法性
//        if (workstation == null) {
//            throw new RuntimeException("工位不存在");
//        }
//        if (workstation.getStatus() == WorkstationStatus.OCCUPIED) {
//            throw new RuntimeException("工位已被占用");
//        }
//
//        // 3. 释放原有工位
//        if (employee.getWorkstationId() != null) {
//            Workstation oldWorkstation = workstationMapper.selectById(employee.getWorkstationId());
//            oldWorkstation.setStatus(WorkstationStatus.AVAILABLE);
//            workstationMapper.updateById(oldWorkstation);
//        }
//
//        // 4. 分配新工位
//        workstation.setStatus(WorkstationStatus.OCCUPIED);
//        workstationMapper.updateById(workstation);
//        employee.setWorkstationId(workstationId);
//        employeeMapper.updateById(employee);
//    }
//
//    @Transactional
//    @Override
//    public void resign(Integer id) {
//        // 1. 查询员工
//        Employee employee = findById(id);
//        employee.setStatus(EmployeeStatus.RESIGNED);
//
//        // 2. 释放工位（如果存在）
//        if (employee.getWorkstationId() != null) {
//            Workstation workstation = workstationMapper.selectById(employee.getWorkstationId());
//            workstation.setStatus(WorkstationStatus.AVAILABLE);
//            workstationMapper.updateById(workstation);
//            employee.setWorkstationId(null);
//        }
//
//        // 3. 更新员工状态
//        employeeMapper.updateById(employee);
//    }
//}